package com.huabo.sql.record;

/**
 * 扩展的数据类型
 */
public enum ExtendType {

    DEFAULT(1, "", "?"),
    PG_JSONB(2, "JSONB", "?::JSONB"),
    PG_JSON(3, "JSON", "?::JSON");

    private final int code;//编码
    private final String type;//类型
    private final String placeholder;//占位符

    private ExtendType(int code, String type, String placeholder) {
        this.code = code;
        this.type = type;
        this.placeholder = placeholder;
    }

    public String valueAtPlaceholder(int code) {
        for (ExtendType extendType : ExtendType.values()) {
            if (code == extendType.getCode()) {
                return extendType.getPlaceholder();
            }
        }
        return null;
    }

    public int valueAtCode(String type) {
        for (ExtendType extendType : ExtendType.values()) {
            if (type.equals(extendType.getType())) {
                return extendType.getCode();
            }
        }
        return -1;
    }

    public String valueOf(int code) {
        for (ExtendType extendType : ExtendType.values()) {
            if (code == extendType.getCode()) {
                return extendType.getType();
            }
        }
        return null;
    }

    public int getCode() {
        return code;
    }

    public String getType() {
        return type;
    }

    public String getPlaceholder() {
        return placeholder;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ExtendType{");
        sb.append("code=").append(code);
        sb.append(", type='").append(type).append('\'');
        sb.append(", placeholder='").append(placeholder).append('\'');
        sb.append('}');
        return sb.toString();
    }
}